package com.hkj.genealogy.genealogydemo.util;


import com.hkj.genealogy.genealogydemo.entity.MemberTree;


import java.util.ArrayList;
import java.util.List;

/**
 * @program: genealogydemo
 * @description: 生成树形结构工具类
 * @author: Hekejian
 * @time: 2020-07-06 01:41
 **/
public class TreeUtil {


    public  static List<MemberTree> recursiveMemberTree(List<MemberTree> memberTree ){
        List<MemberTree> trees=new ArrayList<>();

        for(MemberTree treeNode :memberTree){
            if (treeNode.getPid()==0)
            trees.add(findChildren(treeNode,memberTree));
        }
        return trees;
    }
    public static MemberTree findChildren(MemberTree treeNode,List<MemberTree> treesNodes){
        for (MemberTree it:treesNodes){
            if(treeNode.getId()==it.getPid()){
                if(treeNode.getChildrens()==null){
                    treeNode.setChildrens(new ArrayList<MemberTree>());
                }
                treeNode.getChildrens().add(findChildren(it,treesNodes));
            }
        }
        return treeNode;
    }
}
